<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <p>deferred test</p>

    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
         function waitHandle(){
             var dtd=$.Deferred()  //创建一个deferred对象

             var wait=function(dtd){  //要求传入一个deferred对象
                 var task=function(){
                     console.log('执行完成')
                     dtd.resolve()  //表示异步任务已经完成
                    //  dtd.reject()  // 表示异步任务失败或出错
                 }

                 setTimeout(task,2000)
                 return dtd.promise()  //这里返回 promise 对象
             }

             //注意，这里一定要有返回值
             return wait(dtd)
         }

        var w=waitHandle()  //promise 对象
        $.when(w).then(function(){
            console.log('执行完成 ok 1')
        },function(){
            console.log('失败 err 1')
        })


    </script>
</body>
</html>